<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Catalog extends CI_Controller {
	public function __construct(){
		parent::__construct();
		 if(!$this->session->userdata('is_login')){
            redirect("/admin/auth/login");
	}
}	
	private	$validate_config = array(
	    array(
	        'field' => 'name',
	        'label' => '分类',
	        'rules' => 'required'
	    ),
	);
	public function index(){
		    	$this->load->model("Catalog_model");
		    	$catalogs=$this->Catalog_model->get_all();
		    	$data=array(
		    		"catalogs"=>$catalogs,
		    		"pagename"=>2
		    );
		    	
		    	
				$this->load->view('admin/catalog_index',$data);
	} 
	public function add()
		{	
			$this->load->library('form_validation');
			
	       	$this->form_validation->set_rules($this->validate_config);
	        if ($this->form_validation->run() == FALSE)
	        {	
	        	$catalog=new stdClass();
	        	$catalog->name="";
	        	
	        	
	        	$this->load->model('Catalog_model');
	        	$data=array(
	        		"catalog"=>$catalog,
	        		'title'=>"添加",
	        		
	        		);
	            $this->load->view('admin/catalog_form',$data);
	        }
	        else
	        {	
	        	$data=$this->input->post();
	        	var_dump($data);
	        	$this->load-> model("Catalog_model");
	        	$this->Catalog_model->add_one($data);
	        	

	            redirect("admin/catalog/index");
	        }
	    } 
	   
	    	public function edit($id)
		{	
			$this->load->library('form_validation');
			
	       $this->form_validation->set_rules($this->validate_config);
	        if ($this->form_validation->run() == FALSE)
	        {	
	        	$this->load->model('Catalog_model');
	        	
	        	$article=$this->Catalog_model->get_one($id);
	        	$data=array(
	        		"catalog"=>$this->Catalog_model->get_one($id),
	        		
	        		'title'=>"修改"
	        		);
	            $this->load->view('admin/catalog_form',$data);
	        }
	        else
	        {	
	        	$data=$this->input->post();
	        	$this->load-> model("Catalog_model");
	        	$this->Catalog_model->edit_one($id,$data);
	        	

	            redirect("admin/catalog/index");
	        }
	    }
	    	public function delete($id)
	    {
	    	$this->load->model("Catalog_model");
	    	$this->Catalog_model->delete_one($id);
	    	redirect("admin/catalog/index");

	    }   
}
